home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / strlibs.zip / SUBSTR.C < prev    next >
Text File  |  1993-01-04  |  1KB  |  42 lines

  1.  
  2. /*  File   : substr.c
  3.     Author : Richard A. O'Keefe.
  4.     Updated: 25 May 1984
  5.     Defines: substr()
  6.  
  7.     char *substr(destination, source, offset, length)
  8.  
  9.     copies length bytes from source+offset to destination, stopping
  10.     early if a NUL is encountered.  The difference between this and
  11.  
  12.     strncpy(destination, source+offset, length)
  13.  
  14.     is that if the offset is negative, it has the same effect as 0,
  15.     and if it exceeds strlen(source), it has the same effect as
  16.     strlen(source).
  17.  
  18.     After the substring of source is moved to destination, a NUL byte
  19.     is moved to terminate the string, and the result is a pointer to
  20.     this NUL byte, ready to have new stuff stuck on the end.
  21. */
  22.  
  23. #define NUL '\0'
  24.  
  25. char *substr(dst, src, off, len)
  26.     register char *dst, *src;
  27.     register int off, len;
  28.     {
  29.         while (--off >= 0)
  30.             if (!*src++) {              /* We've hit the end */
  31.                 *dst = NUL;             /* return empty string */
  32.                 return dst;
  33.             }
  34.         while (--len >= 0)
  35.             if (!(*dst++ = *src++)) {   /* We've hit the end */
  36.                 return dst-1;           /* dst is already terminated */
  37.             }
  38.         *dst = NUL;                     /* terminate dst with NUL */
  39.         return dst;
  40.     }
  41.  
  42.